﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using QuanLyTruongHoc_App.MyDataSet;

namespace QuanLyTruongHoc_App
{
    public partial class AddEditGiaoVien : Form
    {
        private String filePath = "";
        private int currID = 0, idCMND, idUser;
        private Boolean editMode = false;
        private DataRow editRow, cmndRow, userRow;

        public AddEditGiaoVien()
        {
            InitializeComponent();
            cb_GioiTinh.SelectedIndex = 0;
            idCMND = -1;
            idUser = -1;
        }

        private void bt_Sua_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void AddEditGiaoVien_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'qLTH.tbl_QuyenUser' table. You can move, or remove it, as needed.
            refreshData();
            refreshID();
            tb_HoDem.Focus();
        }

        private void refreshID()
        {
            if (!editMode)
                currID = PublicLib.getMaxIdFromTbl(qLTH.tbl_GiaoVien.TableName, qLTH.tbl_GiaoVien.idColumn.ColumnName) + 1;
            tb_MaGV.Text = PublicLib.formatGVId(currID);
        }

        private void refreshData()
        {
            // Change Connection String
            this.tbl_HinhThucCanBoTableAdapter.Connection.ConnectionString = 
                this.tbl_LoaiHopDongTableAdapter.Connection.ConnectionString =
                    this.tbl_DanhGiaTableAdapter.Connection.ConnectionString =
                        this.tbl_QuyenUserTableAdapter.Connection.ConnectionString = 
                PublicLib.getStrConn();
            // Fill Data
            this.tbl_HinhThucCanBoTableAdapter.FillByFlag(this.qLTH.tbl_HinhThucCanBo, PConst.FLAG_NONE);
            this.tbl_DanhGiaTableAdapter.FillByFlag(this.qLTH.tbl_DanhGia, PConst.FLAG_NONE);
            this.tbl_LoaiHopDongTableAdapter.FillByFlag(this.qLTH.tbl_LoaiHopDong, PConst.FLAG_NONE);
            this.tbl_QuyenUserTableAdapter.FillByFlag(this.qLTH.tbl_QuyenUser, PConst.FLAG_NONE);

        }

        private void tb_DoiAnh_Click(object sender, EventArgs e)
        {
            OpenFileDialog dialog = new OpenFileDialog();
            dialog.Filter = "JPG, JPEG FILE (*.jpg, *.jpeg)|*.jpg;*.jpeg";
            dialog.Multiselect = false;
            dialog.ShowDialog();

            filePath = dialog.FileName;

            if (filePath == "")
                return;
            FileInfo fi = new FileInfo(filePath);
            if (fi.Length > PConst.MAX_IMG_SIZE)
            {
                MessageBox.Show(MSG.MSG_IMAGE_TOO_BIG);
                filePath = "";
                return;
            }

            pb_Anh.Load(filePath);
        }

        public void setEditInfo(int _idGV)
        {
            try
            {
                DataRow[] newRow;
                MyDataSet.QLTHTableAdapters.tbl_GiaoVienTableAdapter giaoVienAdap = 
                    new QuanLyTruongHoc_App.MyDataSet.QLTHTableAdapters.tbl_GiaoVienTableAdapter();
                giaoVienAdap.Connection.ConnectionString = PublicLib.getStrConn();
                giaoVienAdap.Fill(qLTH.tbl_GiaoVien);

                newRow = qLTH.tbl_GiaoVien.Select(qLTH.tbl_GiaoVien.idColumn.ColumnName + "=" + _idGV.ToString());
                if (newRow.Length == 0)
                    return;
                editRow = newRow[0];

                currID = _idGV;
                tb_HoDem.Text = editRow[qLTH.tbl_GiaoVien.HoDemColumn].ToString();
                tb_Ten.Text = editRow[qLTH.tbl_GiaoVien.TenColumn].ToString();
                dt_NgaySinh.Value = (DateTime)editRow[qLTH.tbl_GiaoVien.NgaySinhColumn];
                cb_GioiTinh.SelectedIndex = int.Parse(editRow[qLTH.tbl_GiaoVien.GioiTinhColumn].ToString());
                // CMND
                MyDataSet.QLTHTableAdapters.tbl_CMNDTableAdapter cmndAdap =
                    new QuanLyTruongHoc_App.MyDataSet.QLTHTableAdapters.tbl_CMNDTableAdapter();
                cmndAdap.Connection.ConnectionString = PublicLib.getStrConn();
                cmndAdap.Fill(qLTH.tbl_CMND);

                //DataRow[] cmndRow;
                idCMND = (int)editRow[qLTH.tbl_GiaoVien.idCMNDColumn];

                cmndRow = qLTH.tbl_CMND.Select(qLTH.tbl_CMND.idColumn + "=" + idCMND.ToString())[0];
                dt_NgayCapCMND.Value = (DateTime)cmndRow[qLTH.tbl_CMND.NgayCapColumn];
                tb_NoiCapCMND.Text = cmndRow[qLTH.tbl_CMND.NoiCapColumn].ToString();
                tb_SoCMND.Text = cmndRow[qLTH.tbl_CMND.SoCMNDColumn].ToString();
                // -------------
                tb_TonGiao.Text = editRow[qLTH.tbl_GiaoVien.TonGiaoColumn].ToString();
                tb_DiaChi.Text = editRow[qLTH.tbl_GiaoVien.DiaChiColumn].ToString();
                tb_DienThoai.Text = editRow[qLTH.tbl_GiaoVien.DienThoaiColumn].ToString();
                tb_Email.Text = editRow[qLTH.tbl_GiaoVien.EmailColumn].ToString();
                cb_CanBo.SelectedValue = editRow[qLTH.tbl_GiaoVien.CanBoColumn];
                cb_DanhGia.SelectedValue = editRow[qLTH.tbl_GiaoVien.DanhGiaColumn];
                cb_HopDong.SelectedValue = editRow[qLTH.tbl_GiaoVien.idHopDongColumn];
                dt_HDHetHan.Value = (DateTime)editRow[qLTH.tbl_GiaoVien.NgayHetHanColumn];

                // Dang Doan
                dt_NgayVaoDoan.Value = (DateTime)editRow[qLTH.tbl_GiaoVien.NgayVaoDoanColumn];
                tb_noiKetNapDoan.Text = editRow[qLTH.tbl_GiaoVien.NoiKetNapDoanColumn].ToString();

                if (editRow[qLTH.tbl_GiaoVien.NgayVaoDangColumn] == DBNull.Value)
                {
                    chk_DangVien.Checked = false;
                    dt_NgayVaoDang.Enabled = false;
                    tb_NoiKetNapDang.Enabled = false;
                }
                else
                {
                    chk_DangVien.Checked = true;
                    dt_NgayVaoDang.Value = (DateTime) editRow[qLTH.tbl_GiaoVien.NgayVaoDangColumn];
                    tb_NoiKetNapDang.Text = editRow[qLTH.tbl_GiaoVien.NoiKetNapDangColumn].ToString();
                }

                rtb_QuaTrinhHoatDong.Text = editRow[qLTH.tbl_GiaoVien.QuaTrinhCongTacColumn].ToString(); ;
                rtb_KyLuat.Text = editRow[qLTH.tbl_GiaoVien.KyLuatColumn].ToString(); ;
                rtb_KhenThuong.Text = editRow[qLTH.tbl_GiaoVien.KhenThuongColumn].ToString();
                rtb_GhiChu.Text = editRow[qLTH.tbl_GiaoVien.GhiChuColumn].ToString();

                // user
                MyDataSet.QLTHTableAdapters.tbl_UserTableAdapter userAdap =
                    new QuanLyTruongHoc_App.MyDataSet.QLTHTableAdapters.tbl_UserTableAdapter();
                userAdap.Connection.ConnectionString = PublicLib.getStrConn();
                userAdap.Fill(qLTH.tbl_User);

                idUser = (int)editRow[qLTH.tbl_GiaoVien.useridColumn];

                userRow = qLTH.tbl_User.Select(qLTH.tbl_User.idColumn.ColumnName + "=" + idUser.ToString())[0];
                tb_TenDangNhap.Text = userRow[qLTH.tbl_User.unameColumn].ToString();
                tb_Pass.Text = userRow[qLTH.tbl_User.upassColumn].ToString();
                cb_Quyen.SelectedValue = userRow[qLTH.tbl_User.utypeColumn];
                // -------------

                // Anh
                if (!(editRow[qLTH.tbl_GiaoVien.AnhColumn] == DBNull.Value))
                {
                    filePath = System.IO.Path.GetTempFileName();
                    FileStream fs = new System.IO.FileStream(filePath, FileMode.Create, FileAccess.Write);
                    Byte []str = (byte[])editRow[qLTH.tbl_GiaoVien.AnhColumn];
                    fs.Write(str, 0, str.Length);
                    fs.Close();

                    pb_Anh.Load(filePath);
                }
                else
                    filePath = "";

                editMode = true;
                tb_TenDangNhap.Enabled = false;
                refreshID();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Lỗi :" + ex.Message);
            }
        }

        private Boolean checkInfo()
        {
            if (tb_TenDangNhap.Text == "")
            {
                MessageBox.Show(MSG.MSG_USERNAME_EMPTY);
                tc_Main.SelectedTab = tabPage3;
                tb_TenDangNhap.Focus();
                return false;
            }

            MyDataSet.QLTHTableAdapters.tbl_UserTableAdapter adap = new QuanLyTruongHoc_App.MyDataSet.QLTHTableAdapters.tbl_UserTableAdapter();
            adap.Connection.ConnectionString = PublicLib.getStrConn();
            if (!editMode && adap.GetDataByUserName(tb_TenDangNhap.Text).Rows.Count > 0)
            {
                MessageBox.Show(MSG.MSG_USERNAME_EXIST);
                tc_Main.SelectedTab = tabPage3;
                tb_TenDangNhap.Focus();
                return false;
            }

            return true;
        }

        private void bt_ThemMoi_Click(object sender, EventArgs e)
        {
            if (!checkInfo())
                return;

            try
            {
                refreshID();

                if (!editMode)
                    editRow = qLTH.tbl_GiaoVien.NewRow();

                editRow[qLTH.tbl_GiaoVien.idColumn] = currID;
                editRow[qLTH.tbl_GiaoVien.HoDemColumn] = tb_HoDem.Text;
                editRow[qLTH.tbl_GiaoVien.TenColumn] = tb_Ten.Text;
                editRow[qLTH.tbl_GiaoVien.NgaySinhColumn] = dt_NgaySinh.Value;
                editRow[qLTH.tbl_GiaoVien.GioiTinhColumn] = cb_GioiTinh.SelectedIndex;
                // CMND
                if (!editMode)
                {
                    cmndRow = qLTH.tbl_CMND.NewRow();
                    idCMND = PublicLib.getMaxIdFromTbl(qLTH.tbl_CMND.TableName, qLTH.tbl_CMND.idColumn.ColumnName) + 1;
                }
                
                cmndRow[qLTH.tbl_CMND.idColumn] = idCMND;
                cmndRow[qLTH.tbl_CMND.NgayCapColumn] = dt_NgayCapCMND.Value;
                cmndRow[qLTH.tbl_CMND.NoiCapColumn] = tb_NoiCapCMND.Text;
                cmndRow[qLTH.tbl_CMND.SoCMNDColumn] = tb_SoCMND.Text;

                MyDataSet.QLTHTableAdapters.tbl_CMNDTableAdapter 
                    cmndAdap = new QuanLyTruongHoc_App.MyDataSet.QLTHTableAdapters.tbl_CMNDTableAdapter();
                cmndAdap.Connection.ConnectionString = PublicLib.getStrConn();

                editRow[qLTH.tbl_GiaoVien.idCMNDColumn] = idCMND;
                // -------------
                editRow[qLTH.tbl_GiaoVien.TonGiaoColumn] = tb_TonGiao.Text;
                editRow[qLTH.tbl_GiaoVien.DiaChiColumn] = tb_DiaChi.Text;
                editRow[qLTH.tbl_GiaoVien.DienThoaiColumn] = tb_DienThoai.Text;
                editRow[qLTH.tbl_GiaoVien.EmailColumn] = tb_Email.Text;
                editRow[qLTH.tbl_GiaoVien.CanBoColumn] = cb_CanBo.SelectedValue;
                editRow[qLTH.tbl_GiaoVien.DanhGiaColumn] = cb_DanhGia.SelectedValue;
                editRow[qLTH.tbl_GiaoVien.idHopDongColumn] = cb_HopDong.SelectedValue;
                editRow[qLTH.tbl_GiaoVien.NgayHetHanColumn] = dt_HDHetHan.Value;

                // Dang Doan
                editRow[qLTH.tbl_GiaoVien.NgayVaoDoanColumn] = dt_NgayVaoDoan.Value;
                editRow[qLTH.tbl_GiaoVien.NoiKetNapDoanColumn] = tb_noiKetNapDoan.Text;

                if (chk_DangVien.Checked)
                {
                    editRow[qLTH.tbl_GiaoVien.NgayVaoDangColumn] = dt_NgayVaoDang.Value;
                    editRow[qLTH.tbl_GiaoVien.NoiKetNapDangColumn] = tb_NoiKetNapDang.Text;
                }
                else
                {
                    editRow[qLTH.tbl_GiaoVien.NgayVaoDangColumn] = DBNull.Value;
                    editRow[qLTH.tbl_GiaoVien.NoiKetNapDangColumn] = "";
                }

                editRow[qLTH.tbl_GiaoVien.QuaTrinhCongTacColumn] = rtb_QuaTrinhHoatDong.Text;
                editRow[qLTH.tbl_GiaoVien.KyLuatColumn] = rtb_KyLuat.Text;
                editRow[qLTH.tbl_GiaoVien.KhenThuongColumn] = rtb_KhenThuong.Text;
                editRow[qLTH.tbl_GiaoVien.GhiChuColumn] = rtb_GhiChu.Text;

                // user
                if (!editMode)
                {
                    userRow = qLTH.tbl_User.NewRow();
                    idUser = PublicLib.getMaxIdFromTbl(qLTH.tbl_User.TableName, qLTH.tbl_User.idColumn.ColumnName) + 1;
                }

                userRow[qLTH.tbl_User.idColumn] = idUser;
                userRow[qLTH.tbl_User.unameColumn] = tb_TenDangNhap.Text;

                if (tb_Pass.Text.Length > 0)
                    userRow[qLTH.tbl_User.upassColumn] = CryptorEngine.GetMD5Hash(tb_Pass.Text); // save MD5 hash in CSDL

                userRow[qLTH.tbl_User.utypeColumn] = cb_Quyen.SelectedValue;
                userRow[qLTH.tbl_User.flagColumn] = PConst.FLAG_NONE;

                MyDataSet.QLTHTableAdapters.tbl_UserTableAdapter userAdap = new QuanLyTruongHoc_App.MyDataSet.QLTHTableAdapters.tbl_UserTableAdapter();
                userAdap.Connection.ConnectionString = PublicLib.getStrConn();

                editRow[qLTH.tbl_GiaoVien.useridColumn] = idUser;
                // -------------

                // Anh
                if (!(filePath == ""))
                {
                    FileStream fs = new System.IO.FileStream(filePath, FileMode.Open, FileAccess.Read);
                    FileInfo fi = new FileInfo(filePath);
                    byte[] str = new byte[fi.Length];
                    long byteread = fs.Read(str, 0, (int)fi.Length);
                    editRow[qLTH.tbl_GiaoVien.AnhColumn] = str;
                }
                else
                    editRow[qLTH.tbl_GiaoVien.AnhColumn] = DBNull.Value;
                // -------------
                editRow[qLTH.tbl_GiaoVien.flagColumn] = PConst.FLAG_NONE;

                MyDataSet.QLTHTableAdapters.tbl_GiaoVienTableAdapter giaoVienAdap = new QuanLyTruongHoc_App.MyDataSet.QLTHTableAdapters.tbl_GiaoVienTableAdapter();
                giaoVienAdap.Connection.ConnectionString = PublicLib.getStrConn();

                if (!editMode)
                {
                    qLTH.tbl_CMND.Rows.Add(cmndRow);
                    qLTH.tbl_User.Rows.Add(userRow);
                    qLTH.tbl_GiaoVien.Rows.Add(editRow);
                }
                
                cmndAdap.Update(cmndRow);
                userAdap.Update(userRow);
                giaoVienAdap.Update(editRow);
                this.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(MSG.MSG_ERROR_INSERT_DB + " :" + ex.Message);
            }
        }

        private void chk_DangVien_CheckedChanged(object sender, EventArgs e)
        {
            dt_NgayVaoDang.Enabled = tb_NoiKetNapDang.Enabled = chk_DangVien.Checked;
        }

        private void bt_IN_Click(object sender, EventArgs e)
        {
            PrintDataGrid.PrintHoSo p = new PrintDataGrid.PrintHoSo();
            p.Title = "Giáo Viên " + tb_HoDem.Text + " " + tb_Ten.Text;
            p.setImage(filePath);

            p.addField("", "THÔNG TIN CƠ BẢN");
            p.addField("Mã giáo viên", tb_MaGV.Text);
            p.addField("Họ tên", tb_HoDem.Text + " " + tb_Ten.Text);
            p.addField("Ngày Sinh", dt_NgaySinh.Value.ToString("dd/MM/yyyy"));
            p.addField("Giới Tính", cb_GioiTinh.Text);

            p.addField("Số CMND", tb_SoCMND.Text);
            p.addField("Ngày cấp", dt_NgayCapCMND.Value.ToString("dd/MM/yyyy"));
            p.addField("Nơi Cấp", tb_NoiCapCMND.Text);
            
            //p.addField("Dân tộc", tb_DanToc.Text);

            p.addField("Tôn Giáo", tb_TonGiao.Text);
            p.addField("Địa chỉ", tb_DiaChi.Text);
            p.addField("Điện thoại", tb_DienThoai.Text);
            p.addField("Email", tb_Email.Text);

            p.addField("", "");

            p.addField("Hình thức cán bộ", cb_CanBo.Text);
            p.addField("Hình thức hợp đồng", cb_HopDong.Text);
            p.addField("Hết hạn", dt_HDHetHan.Value.ToString(PConst.FORMAT_DATE));
            p.addField("Đánh giá", cb_DanhGia.Text);
            //p.addField(PrintDataGrid.PrintHoSo.PAGE_BREAK, "");
            // new page
            p.addField("", "QUÁ TRÌNH CÔNG TÁC");

            p.addField("Ngày vào đoàn", dt_NgayVaoDoan.Value.ToString(PConst.FORMAT_DATE));
            p.addField("Nơi kết nạp", tb_noiKetNapDoan.Text);

            if (chk_DangVien.Checked)
            {
                p.addField("Ngày vào đảng", dt_NgayVaoDang.Value.ToString(PConst.FORMAT_DATE));
                p.addField("Nơi kết nạp", tb_NoiKetNapDang.Text);
            }

            p.addField("Quá trình hoạt động", rtb_QuaTrinhHoatDong.Text);
            // Khen thuong
            p.addField("", "KHEN THƯỞNG - KỶ LUẬT");
            p.addField("Khen thưởng", rtb_KhenThuong.Text);
            p.addField("Kỷ luật", rtb_KyLuat.Text);
            p.addField("Ghi chú", rtb_GhiChu.Text);

            // He thong
            p.addField("", "THÔNG TIN HỆ THỐNG");
            p.addField("Tên đăng nhập", tb_TenDangNhap.Text);
            p.addField("Quyền", cb_Quyen.Text);

            PrintDataGrid.PrintHoSoOptions opt = new PrintDataGrid.PrintHoSoOptions(p);
            opt.MdiParent = this.MdiParent;
            opt.Show();

        }
    }
}
